<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


    /**
     * 成功返回.
     *
     * @param array $data
     * @param string $msg
     * @param int $code
     * @return JsonResponse
     */
    public function responseSuccess(array $data = [], string $msg = '', int $code = 1): JsonResponse
    {
        if (empty($msg)) {
            $msg = '获取成功';
        }

        return response()->json(['code' => $code, 'msg' => $msg, 'data' => $data]);
    }

    /**
     * 失败返回.
     *
     * @param string $msg
     * @param int $code
     * @param array $data
     * @return JsonResponse
     */
    public function responseError(string $msg = '', int $code = 0, array $data = []): JsonResponse
    {
        if (empty($msg)) {
            $msg = '获取失败';
        }

        return response()->json(['code' => $code, 'msg' => $msg, 'data' => $data]);
    }

    /**
     * 自定义返回.
     *
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return JsonResponse
     */
    public function customResponse(int $code, string $msg = '', array $data = []): JsonResponse
    {
        if (empty($msg)) {
            $msg = 'Success';
        }
        if (empty($code)) {
            $code = 1;
        }

        return response()->json(['code' => $code, 'msg' => $msg, 'data' => $data]);
    }

}
